﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.OA;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.OA
{
    public class SystemPostServices : ISystemPostServices
    {
        public SystemPostServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        //新增
        public async Task<bool> AddAsync(SystemPost model)
        {
            //添加数据
            var result = await Db.InsertAsync(model);

            return result;
        }

        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete SystemPost where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
        //删除
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete SystemPost where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        //查询所有
        public async Task<IEnumerable<SystemPost>> GetAllAsync()
        {
            string sql = "select * from SystemPost Order By  Sort asc,CreateTime desc";
            var list = await Db.QueryAllAsync<SystemPost>(sql);
            return list;
        }
        //分页获取
        public async Task<IEnumerable<SystemPost>> GetAsync(int PageIndex, int PageSize, string Code, string Name)
        {
            string sql = "select * from SystemPost where 1=1";
            if (!string.IsNullOrEmpty(Code))
            {
                sql += " and Code like @Code";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name";
            }
            sql += " order by Sort asc,CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<SystemPost>(sql, new { PageIndex, PageSize, Code = $"%{Code}%", Name = $"%{Name}%" });
            return list;
        }
        //获取总条数
        public async Task<int> GetCountAsync(string Code, string Name)
        {
            string sql = "select count(*) from SystemPost where 1=1";
            if (!string.IsNullOrEmpty(Code))
            {
                sql += "and Code like @Code";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                sql += "and Name like @Name";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { Code = $"%{Code}%", Name = $"%{Name}%" });
            return count;
        }
        //修改
        public async Task<bool> UpdateAsync(SystemPost model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
